package com.css.fxfzcpxx.modules.util;

import com.css.fxfzcpxx.base.attachment.entity.AttachmentRecordEntity;
import com.css.fxfzcpxx.base.attachment.service.AttachmentRecordService;
import com.css.fxfzcpxx.constants.DownloadEnum;
import com.css.fxfzcpxx.util.PlatformObjectUtils;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.slw.common.helper.PropsHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Properties;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/05/05
 * @Description: 其他系统下载,预览
 */
@RestController
@Slf4j
@RequestMapping("/other")
public class OtherSysDownController {

    @Autowired
    private AttachmentRecordService attachmentRecordService;

    /**
     * 调用其他系统下载
     * @param name 要下载的文件全名
     * @param id 文件id
     * @param dataOrigin 系统标记,枚举用
     */
    @GetMapping("/Download")
    @Operation(summary = "调用其他系统下载")
    public void otherSysDownload(String name, String id, String dataOrigin, HttpServletResponse response) {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        OutputStream os = null;
        InputStream is = null;
        try {
            AttachmentRecordEntity attachmentRecord=attachmentRecordService.findByAttachmentId(id);
            if(PlatformObjectUtils.isNotEmpty(attachmentRecord)){
                attachmentRecordService.update(attachmentRecord.getId(),attachmentRecord.getDownloadCount()+1);
            }else{
                attachmentRecordService.save(id);
            }
            // 与服务器建立连接
            URL url = new URL(configGroup.getProperty(DownloadEnum.getDownloadEnumPath(dataOrigin))+"/attachment/downLoadFile?attachId="+id) ;
            URLConnection conn = url.openConnection();
            is = conn.getInputStream();
            assert response != null;
            response.reset();
            response.setContentType("application/octet-stream");
            //获取文件名称

            response.setHeader("Content-Disposition", "attachment;filename=\"" + URLEncoder.encode(name,"UTF-8") + "\"");
            // 跨域
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Method", "POST,GET");
            os = response.getOutputStream();
            int flag = 0;
            while ((flag = is.read()) != -1) {
                os.write(flag);
            }
        } catch (Exception e){
            e.printStackTrace();
        }finally{
            try{
                if(is != null){
                    is.close();
                }
                if(os != null){
                    os.flush();
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 调用其他系统pdf
     * @param name 要下载的文件全名
     * @param id 文件id
     * @param dataOrigin 系统标记,枚举用
     */
    @GetMapping("/getPDF")
    @Operation(summary = "调用其他系统pdf")
    public void otherSysGetPDF(String name, String id, String dataOrigin, HttpServletResponse response) {
        String[] split = name.split("\\.");
        String pdfName=split[0]+".pdf";
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        OutputStream os = null;
        InputStream is = null;
        try {
            // 与服务器建立连接
            URL url = new URL(configGroup.getProperty(DownloadEnum.getDownloadEnumPath(dataOrigin))+"/pdf/getPDF?attachId="+id) ;
            URLConnection conn = url.openConnection();
            is = conn.getInputStream();
            assert response != null;
            response.reset();
            response.setContentType("application/octet-stream");
            //获取文件名称

            response.setHeader("Content-Disposition", "attachment;filename=\"" + URLEncoder.encode(pdfName,"UTF-8") + "\"");
            // 跨域
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Method", "POST,GET");
            os = response.getOutputStream();
            int flag = 0;
            while ((flag = is.read()) != -1) {
                os.write(flag);
            }
        } catch (Exception e){
            e.printStackTrace();
        }finally{
            try{
                if(is != null){
                    is.close();
                }
                if(os != null){
                    os.flush();
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
